
let Time = {
    // 获取当前时间戳
    getUnix: function () {
        let date = new Date();
        return date.getTime();
    },
    // 获取今天 0点0分0秒 的时间戳
    getTodayUnix: function () {
        let date = new Date();
        date.setHours(0);
        date.setMinutes(0);
        date.setSeconds(0);
        date.setMilliseconds(0);
        return date.getTime();
    },
    // 获取今年 1月 1日 0点 0分 0秒的时间戳
    getYearUnix: function () {
        let date = new Date();
        date.setMonth(0);
        date.setDate(1);
        date.setHours(0);
        date.setMinutes(0);
        date.setSeconds(0);
        date.setMilliseconds(0);
        return date.getTime();
    },
    // 获取标准年月日
    getLastDae: function (time) {
        let date = new Date();

        // 年份
        let year = date.getFullYear();
        // 月份
        let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : getMonth;
        // 天数
        let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();

        // 返回标准的时间
        return year + '-' + month + '-' + day;

    },
    // 转换时间
    getFormatTime: function (timestamp) {
        let now = this.getUnix();
        let today = this.getTodayUnix();
        let year = this.getYearUnix();

        // 转换为秒级时间戳
        let timer = (now - timestamp) / 1000;

        let tip = '';

        if (timer <= 0) {
            tip = '刚刚';
        } else if (Math.floor(timer / 60) <= 0) {
            tip = '刚刚';
        } else if (Math.floor(timer / 3600)) {
            tip = Math.floor(timer / 60) + '分前';
        } else if (Math.floor(timer / 3600 && (timestamp - today) === 0)) {
            tip = Math.floor(timer / 3600) + '小时前';
        } else if (timer / 86400 <= 31) {
            tip = Math.ceil(timer / 86400) + '天前';
        } else {
            tip = this.getLastDate(timestamp);
        }

        return tip;
    }

}


Vue.directive('time', {
    bind: function (el, binding) {
        el.innerHTML = timer.getFormatTime(binding.value);
        el.__timeout__ == setInterval(function () {
            el.innerHTML = Time.getFormatTime(binding, value);
        }, 60000);
    },
    unbind: function (el) {
        clearInterval(el.__timeout__);
        delete el.__timeout__;
    }
})

